package JAVA300.第十章IO;
/**
 * 将文件拷贝封装成方法
 */

import java.io.*;

public class IO_Study05_method {

    public static void main(String[] args) {
        CopyFile("D:\\Java300_Exp\\src\\JAVA300\\第十章IO\\IO_Study05.java","copyJAVA.txt");

    }

    public static void CopyFile(String srcPath,String destPath){
        File src = new File(srcPath);
        File dest = new File(destPath);

        InputStream is = null;
        OutputStream os = null;

        try {
            is = new FileInputStream(src);
            os = new FileOutputStream(dest);

            int len;
            byte[] flush = new byte[1024];
            while ((len = is.read(flush)) != -1){
                os.write(flush,0,len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //两个
            if (null != os){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (null != is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
}
